package com.rmb.white.service.impl;

import com.rmb.common.model.response.CommonCode;
import com.rmb.common.model.response.ResponseResult;
import com.rmb.mart.entity.white.IpWhitelist;
import com.rmb.mart.utils.IdWorker;
import com.rmb.white.dao.IpWhitelistDao;
import com.rmb.white.service.IpWhitelistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName IpWhitelistServiceImpl
 * @Description 表服务实现类
 * @Author yxs
 * @Date 2021/1/25 9:23
 **/
@Service("ipWhitelistService")
public class IpWhitelistServiceImpl implements IpWhitelistService {
    @Resource
    private IpWhitelistDao ipWhitelistDao;
    @Autowired
    private IdWorker idWorker;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public IpWhitelist queryById(Long id) {
        return this.ipWhitelistDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<IpWhitelist> queryAllByLimit(int offset, int limit) {
        return this.ipWhitelistDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param ipWhitelist 实例对象
     * @return 实例对象
     */
    @Override
    public ResponseResult insert(IpWhitelist ipWhitelist) {
        if (ipWhitelist != null) {
            ipWhitelist.setId(idWorker.nextId());
            System.out.println(ipWhitelist);
            this.ipWhitelistDao.insert(ipWhitelist);
        }
        return new ResponseResult(CommonCode.ADDED_SUCCESSFULLY);
    }

    /**
     * 修改数据
     *
     * @param ipWhitelist 实例对象
     * @return 实例对象
     */
    @Override
    public ResponseResult update(IpWhitelist ipWhitelist) {
        this.ipWhitelistDao.update(ipWhitelist);
        this.queryById(ipWhitelist.getId());
        return new ResponseResult(CommonCode.UPDATEED_SUCCESSFULLY);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.ipWhitelistDao.deleteById(id) > 0;
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<IpWhitelist> findAll() {
        List<IpWhitelist> all = ipWhitelistDao.findAll();
        return all;
    }

    /**
     * 通过主键改变状态为开启
     * * @param id 主键
     * * @return 影响行数
     */
    @Override
    public int startup(Long id) {
        return ipWhitelistDao.startup(id);
    }

    /**
     * 通过主键改变状态为开启
     * @param id
     * @return
     */
    @Override
    public int disable(Long id) {
        return ipWhitelistDao.disable(id);
    }
}